# Global configuration
global:
  scrape_interval:     {{ prometheus_arg.scrape_interval }}
  scrape_timeout:      {{ prometheus_arg.scrape_timeout }}
  evaluation_interval: {{ prometheus_arg.evaluation_interval }}
  external_labels:
    replica: {{ ansible_hostname|upper }}

# Alertmanager configuration
alerting:
  alert_relabel_configs:
    - regex: 'replica|job|ipaddress|metrics_path|scheme|application|domain'
      action: labeldrop
  alertmanagers:
    - static_configs:
      - targets: ['{{ prometheus_server | list | join(":" + prometheus_port.alertmanager + "', '") }}:{{ prometheus_port.alertmanager }}']

# Rule configuration
rule_files:
  - {{ prometheus_conf_path }}/rules/*.rules

# Scrape configuration
scrape_configs:
{% if prometheus_consul_server is defined and prometheus_consul_token is defined %}
  - job_name: 'unAuthenticate exporters'
    tls_config:
      insecure_skip_verify: true
    consul_sd_configs:
      - server: '{{ prometheus_consul_server }}'
        token: {{ prometheus_consul_token }}
        refresh_interval: {{ prometheus_arg.refresh_interval }}
        services: ['{{ "_exporter', '".join(vars["unauthenticate_target_services"]) }}_exporter']
        scheme: {{ consul_public_http_prot }}
        tls_config:
          insecure_skip_verify: true
    relabel_configs:
      - source_labels: [__meta_consul_service_address]
        target_label: 'ipaddress'
      - regex: __meta_consul_service_metadata_(.+)
        action: labelmap
      - source_labels: [__meta_consul_service]
        replacement: '${1}'
        target_label: 'service'
        regex: '([^=]+)_exporter'
      - source_labels: [__meta_consul_service_metadata_metrics_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - source_labels: [__meta_consul_service_metadata_scheme]
        action: replace
        target_label: __scheme__
        regex: (.+)
{% if prometheus_server | length > 3 %}
      - source_labels: [__address__]
        regex: '.+[{{ ip_sequence | list | unique | sort | join("") }}]:.+'
        action: 'drop'
{% endif %}
  - job_name: 'Authenticate exporters'
    basic_auth:
      username: 'prometheus'
      password: 'tj@VH9ECytRF'
    tls_config:
      insecure_skip_verify: true
    consul_sd_configs:
      - server: '{{ prometheus_consul_server }}'
        token: {{ prometheus_consul_token }}
        refresh_interval: {{ prometheus_arg.refresh_interval }}
        services: ['{{ "_exporter', '".join(vars["authenticate_target_services"]) }}_exporter']
        scheme: {{ consul_public_http_prot }}
        tls_config:
          insecure_skip_verify: true
    relabel_configs:
      - source_labels: [__meta_consul_service_address]
        target_label: 'ipaddress'
      - regex: __meta_consul_service_metadata_(.+)
        action: labelmap
      - source_labels: [__meta_consul_service]
        replacement: '${1}'
        target_label: 'service'
        regex: '([^=]+)_exporter'
      - source_labels: [__meta_consul_service_metadata_metrics_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - source_labels: [__meta_consul_service_metadata_scheme]
        action: replace
        target_label: __scheme__
        regex: (.+)
{% if prometheus_server | length > 3 %}
      - source_labels: [__address__]
        regex: '.+[{{ ip_sequence | list | unique | sort | join("") }}]:.+'
        action: 'drop'
{% endif %}
{% if prometheus_prober_exporters is defined %}
  - job_name: 'Probers'
    consul_sd_configs:
      - server: '{{ prometheus_consul_server }}'
        token: {{ prometheus_consul_token }}
        refresh_interval: {{ prometheus_arg.refresh_interval }}
        services: ['{{ "_prober', '".join(vars["prometheus_prober_exporters"]) }}_prober']
        scheme: {{ consul_public_http_prot }}
        tls_config:
          insecure_skip_verify: true
    relabel_configs:
      - regex: __meta_consul_service_metadata_(.+)
        action: labelmap
      - source_labels: [__meta_consul_service_metadata_target]
        target_label: '__param_target'
      - source_labels: [__meta_consul_service_metadata_module]
        target_label: '__param_module'
      - source_labels: [__meta_consul_service_metadata_address]
        action: replace
        target_label: __address__
        regex: (.+)
      - source_labels: [__meta_consul_service_metadata_metrics_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
{% endif %}
{% endif %}
{% if prometheus_kubernetes is defined %}
{% for host in prometheus_kubernetes %}
  - job_name: 'Kubernetes kubelet - {{ host.apiserver | default("127.0.0.1") }}'
    scheme: https
    bearer_token: '{{ host.token | default("xxx") }}'
    tls_config:
      insecure_skip_verify: true
    kubernetes_sd_configs:
      - role: node
        api_server: 'https://{{ host.apiserver | default("127.0.0.1") }}'
        bearer_token: '{{ host.token | default("xxx") }}'
        tls_config:
          insecure_skip_verify: true
    relabel_configs:
    - action: labelmap
      regex: __meta_kubernetes_node_label_(.+)
    - target_label: __address__
      replacement: {{ host.apiserver | default("127.0.0.1") }}
    - source_labels: [__address__]
      action: replace
      target_label: application
      replacement: Kubernetes
    - source_labels: [__address__]
      action: replace
      target_label: service
      replacement: kubelet
    - source_labels: [__meta_kubernetes_node_name]
      regex: (.+)
      target_label: __metrics_path__
      replacement: /api/v1/nodes/${1}/proxy/metrics
  - job_name: 'Kubernetes cadvisor - {{ host.apiserver | default("127.0.0.1") }}'
    scheme: https
    bearer_token: '{{ host.token | default("xxx") }}'
    tls_config:
      insecure_skip_verify: true
    kubernetes_sd_configs:
      - role: node
        api_server: 'https://{{ host.apiserver | default("127.0.0.1") }}'
        bearer_token: '{{ host.token | default("xxx") }}'
        tls_config:
          insecure_skip_verify: true
    relabel_configs:
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)
      - target_label: __address__
        replacement: {{ host.apiserver | default("127.0.0.1") }}
      - source_labels: [__meta_kubernetes_node_name]
        regex: (.+)
        target_label: __metrics_path__
        replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
{% endfor %}
{% endif %}
